package com.hzncc.scaffold.controller.Rear.OnePlusX;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Bo.ExpertapplyBo;
import com.hzncc.scaffold.entity.Bo.TrainerListBo;
import com.hzncc.scaffold.entity.Other.Expertapply;
import com.hzncc.scaffold.entity.Other.Oneplusxtraining;
import com.hzncc.scaffold.entity.Other.User;
import com.hzncc.scaffold.entity.Other.Usertag;
import com.hzncc.scaffold.entity.Vo.*;
import com.hzncc.scaffold.mapper.ExpertapplyMapper;
import com.hzncc.scaffold.mapper.OneplusxrecordMapper;
import com.hzncc.scaffold.service.*;
import com.hzncc.scaffold.utils.NumberUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: DrunkBear
 * @Date: 2022-05-24 17:14
 * @Description: null
 */
@Api(tags = "1+X管理-专家库")
@RequestMapping("/manage/expert")
@RestController
public class ExpertLibraryController {


    @Autowired
    ExpertapplyService expertapplyService;

    @Autowired
    TrainerapplyService trainerapplyService;

    @Autowired
    UserService userService;

    @Autowired
    UsertagService usertagService;

    @Autowired
    TagService tagService;

    @Autowired
    OneplusxtrainingService oneplusxtrainingService;

    @Autowired
    OneplusxrecordMapper oneplusxrecordMapper;

    @Autowired
    ExpertapplyMapper expertapplyMapper;

    @GetMapping("/list")
    @ApiOperation("专家库列表")
    public ResultT list(Integer pageNum, Integer page, Integer pageSize, String keyword, Integer status) {
        Integer pageData = NumberUtils.pageNum(page, pageNum);
        ArrayList<ExpertListVo> expertListVos = new ArrayList<>();

        QueryWrapper<Expertapply> qw = new QueryWrapper<>();
        if (status != null) {
            qw.eq("expertapply.status", status);
        }

        if (StrUtil.isNotEmpty(keyword)) {
            qw.like("realname", keyword);
        }

        if (pageData != null && pageSize != null && pageData > 0 && pageSize > 0) {
            PageHelper.startPage(pageData, pageSize);
        }
        List<ExpertapplyBo> expertapplyBos = expertapplyMapper.listBo(qw);
        for (ExpertapplyBo expertapply : expertapplyBos) {
            ExpertListVo expertListVo = BeanUtil.toBean(expertapply, ExpertListVo.class);

            PublisherVo publisherVo = new PublisherVo();
            User user = new User();

            if (userService.getById(expertapply.getUserId()) != null) {
                user = userService.getById(expertapply.getUserId());
            }

            if (userService.getById(expertapply.getPublisherId()) != null) {
                User publisher = userService.getById(expertapply.getPublisherId());

                publisherVo = BeanUtil.toBean(publisher, publisherVo.getClass());
            }

            expertListVo.setUser(user);
            expertListVo.setPublisher(publisherVo);

            expertListVos.add(expertListVo);
        }

//        PageInfo<ExpertListVo> info = new PageInfo<>();
//        info.setList(expertListVos);
//        info.setTotal(new PageInfo<>(expertapplyBos).getTotal());
        HashMap<String,Object> map = new HashMap<>();
        map.put("rows",expertListVos);
        map.put("count",new PageInfo<>(expertapplyBos).getTotal());
        return ResultT.success(map);
    }


    @PostMapping("/updateStatus")
    @ApiOperation("修改专家状态 通过或不通过")
    public ResultT updateStatus(@RequestBody UpdateStatusVo updateStatusVo) {

        Integer applyId = updateStatusVo.getApplyId();
        Integer status = updateStatusVo.getStatus();


        if (expertapplyService.getById(applyId) == null) {
            return ResultT.fail("数据错误");
        }
        Expertapply expertapply = expertapplyService.getById(applyId);
        expertapply.setStatus(status);
        boolean update = expertapplyService.updateById(expertapply);

        if (update) {
            return ResultT.success("修改成功");
        } else {
            return ResultT.fail("修改失败");
        }
    }

    @GetMapping("/trainerList")
    @ApiOperation("考核师 培训师 考务人员列表 1/考核培训 2/师资培训 3/考务培训 4/1+x考试")
    public ResultT trainerList(Integer pageNum, Integer pageSize, Integer type, String keyword) {
        if (type == null) {
            return ResultT.fail("数据错误");
        }

        QueryWrapper<Object> qw = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(keyword)) {
            qw.like("`owner`.`realname`", keyword);
        }
        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<TrainerListBo> trainerListBos = oneplusxrecordMapper.trainerList(qw, type);
        ArrayList<TrainerListVo> trainerListVos = new ArrayList<>();


        for (TrainerListBo trainerListBo : trainerListBos) {
            TrainerListVo trainerListVo = BeanUtil.toBean(trainerListBo, TrainerListVo.class);
            OwnerVo ownerVo = new OwnerVo();
            ownerVo.setId(trainerListBo.getOwnerId());
            ownerVo.setRealname(trainerListBo.getOwnerRealname());
            ownerVo.setGender(trainerListBo.getOwnerGender());
            ownerVo.setIdCardNum(trainerListBo.getOwnerIdCardNum());
            ownerVo.setTelephone(trainerListBo.getOwnerTelephone());
            ownerVo.setAccount(trainerListBo.getOwnerAccount());
            ownerVo.setInstituteName(trainerListBo.getOwnerInstituteName());
            ownerVo.setAddress(trainerListBo.getOwnerAddress());
            trainerListVo.setOwner(ownerVo);

            if (trainerListBo.getOnePlusXTrainingId() != null) {
                if (oneplusxtrainingService.getById(trainerListBo.getOnePlusXTrainingId()) != null) {
                    Oneplusxtraining oneplusxtraining = oneplusxtrainingService.getById(trainerListBo.getOnePlusXTrainingId());

                    OnePlusXTrainingVo onePlusXTrainingVo = new OnePlusXTrainingVo();
                    onePlusXTrainingVo.setId(oneplusxtraining.getId());
                    onePlusXTrainingVo.setTitle(oneplusxtraining.getTitle());

                    trainerListVo.setOnePlusXTraining(onePlusXTrainingVo);
                }
            }

            LambdaQueryWrapper<Usertag> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Usertag::getUserId, trainerListBo.getUserId());
            if (type == 1) {
                lqw.eq(Usertag::getTagId, 14);
            }
            if (type == 2) {
                lqw.eq(Usertag::getTagId, 15);
            }
            if (type == 3) {
                lqw.eq(Usertag::getTagId, 13);
            }
            List<Usertag> usertagList = usertagService.list(lqw);

            if (CollUtil.isNotEmpty(usertagList)) {
                trainerListVo.setQualified(true);
            } else {
                trainerListVo.setQualified(false);
            }


            trainerListVos.add(trainerListVo);
        }

//        PageInfo<TrainerListVo> info = new PageInfo<>();
//        info.setList(trainerListVos);
//        info.setTotal(new PageInfo<>(trainerListBos).getTotal());
        HashMap<String,Object> map = new HashMap<>();
        map.put("rows",trainerListVos);
        map.put("count",new PageInfo<>(trainerListBos).getTotal());
        return ResultT.success(map);
    }


    @PostMapping("/qualify")
    @ApiOperation("修改资格")
    public ResultT qualify(@RequestBody UpdateQualifyVo updateQualifyVo) {

        Integer status = updateQualifyVo.getStatus();
        Integer type = updateQualifyVo.getType();
        Integer userId = updateQualifyVo.getUserId();


        if (userId == null || type == null || status == null) {
            return ResultT.fail("数据错误");
        }

        if (userService.getById(userId) == null) {
            return ResultT.fail("数据错误,该用户不存在");
        }

        //恢复资格
        if (status == 1) {
            Usertag usertag = new Usertag();
            usertag.setUserId(userId);
            if (type == 1) {
                usertag.setTagId(14);
            }
            if (type == 2) {
                usertag.setTagId(15);
            }
            if (type == 3) {
                usertag.setTagId(13);
            }
            boolean save = usertagService.save(usertag);
            if (save) {
                return ResultT.success("操作成功");
            } else {
                return ResultT.fail("操作失败");
            }
        }

        //取消资格
        if (status == 2) {
            LambdaQueryWrapper<Usertag> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Usertag::getUserId, userId);
            if (type == 1) {
                lqw.eq(Usertag::getTagId, 14);
            }
            if (type == 2) {
                lqw.eq(Usertag::getTagId, 15);
            }
            if (type == 3) {
                lqw.eq(Usertag::getTagId, 13);
            }
            boolean remove = usertagService.remove(lqw);
            if (remove) {
                return ResultT.success("操作成功");
            } else {
                return ResultT.fail("操作失败");
            }
        }
        return ResultT.fail("数据错误");
    }


    @GetMapping("/detail")
    @ApiOperation("获取专家库详情")
    public ResultT detail(Integer applyId) {
        if (expertapplyService.getById(applyId) == null) {
            return ResultT.fail("数据错误");
        }

        Expertapply expertapply = expertapplyService.getById(applyId);
        ExpertApplyDetailVo expertApplyDetailVo = BeanUtil.toBean(expertapply, ExpertApplyDetailVo.class);

        User user = new User();
        if (userService.getById(expertapply.getUserId()) != null) {
            user = userService.getById(expertapply.getUserId());
        }
        expertApplyDetailVo.setUser(user);

        return ResultT.success(expertApplyDetailVo);
    }

    @PostMapping("/delete")
    @ApiOperation("删除专家")
    public ResultT delete(ApplyIdVo applyIdVo) {

        if (expertapplyService.getById(applyIdVo.getApplyId()) == null) {
            return ResultT.fail("数据错误");
        }

        boolean remove = expertapplyService.removeById(applyIdVo.getApplyId());

        if (remove) {
            return ResultT.success("操作成功");
        } else {
            return ResultT.fail("操作失败");
        }

    }
}
